package com.teatime.client.view;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageGridAdapter extends BaseAdapter {

	private List<Bitmap> mPhotos;
	private Context mContext;
	private Bitmap  mFakeImage;
	private int     mMovingPos;
	private int     mDstPos;
	private int     mImageWidth;
	private int     mImageHeight;
	public ImageGridAdapter(Context context, List<Bitmap> photos) {
		super();
		mContext = context;
		mPhotos  = photos;
		mMovingPos = AdapterView.INVALID_POSITION;
		mDstPos    = AdapterView.INVALID_POSITION;
		mImageWidth = 100;
		mImageHeight = 100;
	}
	
	@Override
	public int getCount() {
		if (mPhotos == null) {
			return 0;
		}
		return mPhotos.size();
		
	}

	private ImageView createImageView() {
		ImageView iv;
		iv = new ImageView(mContext);
        iv.setLayoutParams(new GridView.LayoutParams(100, 100));
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return iv;
	}
	
	@Override
	public Object getItem(int position) {
		if (mPhotos == null) {
			return position;
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView iv;
		Bitmap bmp;
		if (mPhotos == null) {
			return null;
		}
		if (position < 0 || position >= mPhotos.size()) {
			return null;
		}
		
		if (convertView == null) {
            iv = createImageView();
		}
		else {
			iv = (ImageView) convertView;
		}
		
		if (mMovingPos == position) {
			if (mDstPos != AdapterView.INVALID_POSITION) {
				bmp = mPhotos.get(mDstPos);
			}
			else {
				bmp = getFake();
			}
		}
		else if (mDstPos == position) {
			bmp = getFake();
		}
		else {
			bmp = mPhotos.get(position);
		}

		iv.setImageBitmap(bmp);
		return iv; 
	}
	
	public Bitmap getFake() {
		if (mPhotos.size() <= 0) {
			return null;
		}
		
		if (mFakeImage == null) {
			mFakeImage = Bitmap.createBitmap(mImageWidth, 
					mImageHeight, Config.ARGB_8888);
			mFakeImage.eraseColor(Color.TRANSPARENT);
		}
		return mFakeImage;
	}
	
	public void setMoving(int position) {
		mMovingPos = position;
	}
	
	public void preExchange(int dstPos) {
		if (mMovingPos != AdapterView.INVALID_POSITION) {
			mDstPos  = dstPos;
		}
	}
	
	public void noPreExchange() {
		mDstPos = AdapterView.INVALID_POSITION;
	}
	
	public void restore() {
		mDstPos = AdapterView.INVALID_POSITION;
		mMovingPos = AdapterView.INVALID_POSITION;
	}
	
	public void exchange(int srcPos, int dstPos) {
		restore();
		if (mPhotos == null
			|| mPhotos.size() <= 0) {
			return;
		}
		
		if (mPhotos.size() <= srcPos
			|| mPhotos.size() <= dstPos
			|| srcPos < 0
			|| dstPos < 0
			|| srcPos == dstPos) {
			return;
		}
		
		Bitmap tmp = mPhotos.get(srcPos);
		mPhotos.set(srcPos, mPhotos.get(dstPos));
		mPhotos.set(dstPos, tmp);
	}
	
	public Bitmap getImage(int position) {
		if (mPhotos == null || mPhotos.size() < position) {
			return null;
		}
		return mPhotos.get(position);
	}

}
